import { getWallet } from 'api/user'

const money = {
  state: {
    diamond: 0,
    // frozen: 0,
    // frozen_type: '',
    normal: 0,
    valid: 0,
    showDialog: false
  },

  mutations: {
    SET_DIA: (state, diamond) => {
      state.diamond = diamond
    },
    SET_NORMAL: (state, normal) => {
      state.normal = normal
    },
    SET_VALID: (state, valid) => {
      state.valid = valid
    },
    SET_DIALOG: (state, showDialog) => {
      state.showDialog = showDialog
    }
  },

  actions: {
    // 获取价格
    GetMoney({ commit }) {
      return new Promise((resolve, reject) => {
        getWallet()
        .then(res => {
          commit('SET_DIA', res.wallet.diamond);
          commit('SET_NORMAL', res.wallet.normal);
          commit('SET_VALID', res.wallet.valid);
          resolve(res)
        })
        .catch(err => {
          reject(err)
        })
      })
    },

    // 检查语点数量
    CheckMoney({ commit, state }, type) {
      return new Promise((resolve, reject) => {
        getWallet()
        .then(res => {
          const wallet = res.wallet
          commit('SET_DIA', wallet.diamond);
          commit('SET_NORMAL', wallet.normal);
          commit('SET_VALID', wallet.valid);
          for (const key in type) {
            if (wallet[key] < type[key]) {
              resolve(false)
            } else {
              resolve(true)
            }
          }
        })
        .catch(err => {
          if (err.code === -1) {
            reject(err)
          } else {
            for (const key in type) {
              if (state[key] < type[key]) {
                resolve(false)
              } else {
                resolve(true)
              }
            }
          }
        })
      })
    },

    // 弹框
    showMDialog({ commit }) {
      commit('SET_DIALOG', true)
    },

    // 隐藏
    hideMDialog({ commit }) {
      commit('SET_DIALOG', false)
    }
  }
}

export default money;
